import { gif } from "./TS/BASE/spineANDgif/gif";
import GeZiManager from "./TS/Manager/GeZiManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class xgcol extends cc.Component {

    xg:string[]=[]//记录效果
    xginitx:number=240//0+i*xginitx=位置,也是滑动的距离
    xgvolicty:number=1//滑动速度,1秒内滑到对应位置
    width:number=150//初始大小,以这个为基准scale=1
    hight:number=150//
    wzmessage:number[][]=[[0,0,1],[240,15,0.9],[480,30,0.8],[720,45,0.7],[860,33,0.6]]//记录x,y,scale在这里调位置
    @property(cc.SpriteFrame)
    picture: cc.SpriteFrame[] = [];
    @property(cc.Prefab)
    richTextPrefab: cc.Prefab = null;//预设体
    @property(cc.RichText)
    richTextArray: cc.RichText[] = [];//存储richtext节点,.scale修改大小
    @property(Number)
    disappear_positionx:number=0
    @property(Number)
    disappear_velocity:number=0.2//1消失的速度,和4出生的速度
    // @property(Array)

   c () 
    {
        // let xg=["0","1","2","3"]
        // for(let i=0;i<4;i++)
        // {
        //     this.richTextArray.push(this.spawnRichText(this.wzmessage[i][0],this.wzmessage[i][1],this.width,this.hight,this.wzmessage[i][2],xg[i]))
        // }

        //init使用举例,-1是没有效果,0-7是有效果
        console.log(GeZiManager.TNJM)
        let txg:string[]=["1",GeZiManager.TNJM[1].toString(),GeZiManager.TNJM[2].toString(),GeZiManager.TNJM[3].toString()]
        console.log(txg.length)
        this.initxg(txg)
    }

    spawnRichText(setx:number,sety:number,width:number,hight:number,scale:number,xg:string)//生成节点,x,y位置,w,h,默认大小,xg效果
    {
        let xgnum:number=parseInt(xg)
        const richtext=cc.instantiate(this.richTextPrefab)
        // richtext.getComponent(cc.Sprite).spriteFrame=this.picture[xgnum]
        richtext.getChildByName("xg").getComponent(gif).spriteFrame=this.picture[xgnum]
        richtext.getComponent(cc.RichText).string=xg
        this.addxg(xg)
        richtext.setPosition(cc.v2(setx,sety))
        this.node.addChild(richtext)
        //设置文本大小,以及逐渐出生
        richtext.setContentSize(width,hight)
        richtext.opacity=0
        richtext.scale=scale
        cc.tween(richtext).to(this.disappear_velocity, { opacity: 255 }).start();
       if(scale==0.6){ cc.tween(richtext).to(1,{ position: cc.v3(this.wzmessage[3][0],this.wzmessage[3][1],0)}).start();}
        //返回节点push进去记录
        return richtext.getComponent(cc.RichText)
    }
//移动
    richtextmove(nextxg:number)
    {
        let change = -this.xginitx
        for (let i = 0; i < this.richTextArray.length; i++) 
        {
            //头位置不动逐渐消失
            if(this.richTextArray[i].node.position.x==this.disappear_positionx)
            {
                cc.tween(this.richTextArray[i].node).to(this.disappear_velocity, { opacity: 0 }).call(()=>{
                    //头位置不动消去xg
                    this.lerpMove(this.richTextArray[i].node, change,i,nextxg)
                }).start();
                continue
            }
            this.lerpMove(this.richTextArray[i].node, change,i,nextxg)

        }
    }

    lerpMove(node: cc.Node, change: number,wz:number,nextxg:number) {
        //头位置不动消去xg
        if(node.position.x==this.disappear_positionx)
        {console.log(node.position)
            node.destroy()
            this.richTextArray=this.richTextArray.slice(1)
            this.subtractxg()
            this.richTextArray.push(this.spawnRichText(this.wzmessage[4][0],this.wzmessage[4][1],this.width,this.hight,this.wzmessage[4][2],nextxg.toString()))
            return
        }
        let startX = node.position.x;
        let startY = node.position.y;
        let endX=startX+change
        let endY=startY-15
        cc.tween(node).to(this.xgvolicty, { position: cc.v3(endX,endY,0), scale: this.richTextArray[wz-1].node.scale}).start();
    }
//尾加入效果
    addxg(xg:string)
    {
        this.xg.push(xg)
    }
//头减去效果
    subtractxg()
    {
        // console.log(this.xg.length)
        this.xg=this.xg.slice(1)
        // console.log(this.xg.length)
    }
//初始化效果
    initxg(xg:any)
    {
        this.xg=xg
        let len=xg.length
        for(let i=0;i<len;i++)
        {
            this.richTextArray.push(this.spawnRichText(this.wzmessage[i][0],this.wzmessage[i][1],this.width,this.hight,this.wzmessage[i][2],this.xg[i]))
        }
    }
}
